//
//  NemoFaceView.m
//  NemoSDKDemo
//
//  Created by 杨旭东 on 2018/7/12.
//  Copyright © 2019 JackYang. All rights reserved.
//

#import "NemoFaceView.h"
#import "NSString+Nemo.h"
#import "NemoFaceUserInfo.h"

@interface NemoFaceView ()

@property (nonatomic, strong) NemoFacePosition *position;

@property (nonatomic, strong) UIImageView *faceHeader;

@property (nonatomic, strong) UIImageView *faceBorder;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, copy) void (^expireBlock)(NemoFaceView *);

@property (nonatomic, assign) NSTimeInterval interval;

@property (nonatomic, strong) NemoFaceUserInfo *faceInfo;

@end



@implementation NemoFaceView

+ (NemoFaceView *)addFacePosition:(NemoFacePosition *)position expire:(void (^)(NemoFaceView *))expire{
    NemoFaceView *face = [[NemoFaceView alloc] initWithFaces:position];
    face.expireBlock = expire;
    return face;
}

- (instancetype)initWithFaces:(NemoFacePosition *)position{
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
    
        self.interval = 0;
        self.faceHeader = [[UIImageView alloc] init];
        self.faceHeader.backgroundColor = [UIColor clearColor];
        [self addSubview:self.faceHeader];
        
        self.faceBorder = [[UIImageView alloc] init];
        self.faceBorder.image = [UIImage imageNamed:@"face_detect_scan"];
        self.faceBorder.backgroundColor = [UIColor clearColor];
        [self addSubview:self.faceBorder];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.backgroundColor = [UIColor clearColor];
        self.titleLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self addSubview:self.titleLabel];
        
        [self _updateSubviewFrame:position];
        self.faceId = position.faceId;
        long faceId = position.faceId;
        if (faceId != -1) {
            __weak NemoFaceView *weakself = self;
            [NemoFaceUserInfo faceUserInfoWithfaceUserId:faceId
                                                   block:^(NemoFaceUserInfo * _Nonnull info, NSError * _Nonnull error) {
                                                       if (error == nil) {
                                                            weakself.faceInfo = info;
                                                           [weakself _updateFaceInfo];
                                                       }
                                                       
                                                   }];
            
        }
    }
    return self;
}

- (void)updateFacePosition:(NemoFacePosition *)position{
    [self _updateSubviewFrame:position];
}

- (void)_updateSubviewFrame:(NemoFacePosition *)position{
    self.position = position;
    if (!self.timer) {
        self.timer  =[NSTimer scheduledTimerWithTimeInterval:.5f target:self selector:@selector(_updateTimestamp) userInfo:nil repeats:YES];
    }
    
    CGPoint faceOrigin = CGPointMake(position.left, position.top);
    CGSize faceSize = CGSizeMake(position.right - position.left, position.bottom - position.top);
    CGSize headerSize = (faceSize.width > 360)?CGSizeMake(165, 78):CGSizeMake(125, 60);
    self.frame = CGRectMake(faceOrigin.x, faceOrigin.y - headerSize.height - 10, faceSize.width, faceSize.height + headerSize.height + 10);
    if (position.faceId == -1) {
        self.faceHeader.hidden = YES;
        self.titleLabel.hidden = YES;
    }else{
        self.faceHeader.hidden = NO;
        self.faceHeader.frame = CGRectMake((faceSize.width - headerSize.width) * .5f, 0, headerSize.width, headerSize.height);
        self.faceHeader.image = [UIImage imageNamed:(faceSize.width > 360)?@"face_header_big":@"face_header_small"];
    }
    
    self.faceBorder.frame = CGRectMake(0, headerSize.height + 10, faceSize.width, faceSize.height);
    [self _updateFaceInfo];
    self.interval = 0;
}

- (void)_updateTimestamp{
    if (self.interval == 2) {
        [self.timer invalidate];
        self.timer = nil;
        
        if (self.expireBlock) {
            self.expireBlock(self);
        }
    }
    self.interval = self.interval + 1;
}

- (long)getFaceId{
    return self.position.faceId;
}

- (void)_updateFaceInfo{
    if (self.faceInfo && !self.faceHeader.hidden) {
        CGSize faceSize = CGSizeMake(self.position.right - self.position.left, self.position.bottom - self.position.top);
        self.titleLabel.hidden = NO;
        self.titleLabel.frame = CGRectMake(self.faceHeader.frame.origin.x + 5, self.faceHeader.frame.origin.y + 5, self.faceHeader.frame.size.width - 10, self.faceHeader.frame.size.height - 10);
        self.titleLabel.font = [UIFont systemFontOfSize:faceSize.width>360?18.0:15.0];
        self.titleLabel.attributedText = self.faceInfo.userName.nemo_appendFont(self.faceInfo.userTitle, faceSize.width>360?14.0:11.0);
    }
}

@end















